package com.lightsh.sms;

public class SignatureHelper {
	public static final String SMS_CONTENT_HEADER = "|[";	
	public static final String SIGN_KEY = "!@#$%^&*()?><";
	
	public static final int SIGN_LENGTH = 10;
	
	public static final String PHONE_AREA_CODE_HEADER = "+86";
	public static final String _PHONE_AREA_CODE_HEADER = "86";
	
	private static final boolean isLegalSmsContentHeader(String sms){
		return sms != null && sms.startsWith(SMS_CONTENT_HEADER);
	}
	
	public static final String getPhoneNumberWithoutAreaCode(String phoneNumber){
		if(phoneNumber != null){
			if(phoneNumber.startsWith(PHONE_AREA_CODE_HEADER)){
				return phoneNumber.substring(PHONE_AREA_CODE_HEADER.length());
			}else if(phoneNumber.startsWith(_PHONE_AREA_CODE_HEADER)){
				return phoneNumber.substring(_PHONE_AREA_CODE_HEADER.length());
			}
		}
		return phoneNumber;
	}
	
	public static final boolean isLegalSign(SmsInfo sms){
		String content = sms.getSmsbody();
		String phoneNumber = sms.getPhoneNumber();
		if(content != null && phoneNumber != null 
				&& isLegalSmsContentHeader(content)
				&& (content.length() > SMS_CONTENT_HEADER.length() + SIGN_LENGTH)){
			phoneNumber = getPhoneNumberWithoutAreaCode(phoneNumber);
			String original = content.substring(SMS_CONTENT_HEADER.length() + SIGN_LENGTH);
			return content.startsWith(SMS_CONTENT_HEADER + Md5Utility.MD5Encode(SIGN_KEY + original).substring(0, SIGN_LENGTH));
		}
		return false;
	}
}
